package jdbc.utils;

import org.junit.jupiter.api.Test;

import java.sql.*;

/**
 * @author DCSGO
 * @version 1.0
 * JDBCUtils 的使用
 * dml select
 */
public class JDBCUtils_Use {
    @Test
    public void test() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtils.getConnection();
            //insert                  id name sex birthday phone_number
            String sql = "insert into performer values (null,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "周星驰");
            preparedStatement.setString(2, "男");
            preparedStatement.setDate(3, new Date(8000000L));
            preparedStatement.setString(4, "12345678910");
            int rows = preparedStatement.executeUpdate();
            System.out.println(rows > 0 ? "insert 成功！" : "insert 失败！");

            //update
            rows = preparedStatement.executeUpdate("update performer set `name` = '郭富城' where id = 1");
            System.out.println(rows > 0 ? "update 成功！" : "update 失败！");

            //select
            resultSet = preparedStatement.executeQuery("select * from performer");
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String sex = resultSet.getString("sex");
                Date birthday = resultSet.getDate("birthday");
                String phone_number = resultSet.getString("phone_number");
                System.out.println(id + "\t" + name + "\t" + sex + "\t" + birthday + "\t" + phone_number);
            }

            //delete
            rows = preparedStatement.executeUpdate("delete from performer where id = 3");
            System.out.println(rows > 0 ? "delete 成功！" : "delete 失败！");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(resultSet, preparedStatement, connection);
        }
        /*
         * insert 成功！
         * update 成功！
         * 1	郭富城	男	1990-04-25	11223344556
         * 3	jack	男	2000-09-30	12345678901
         * 6	周星驰	男	1970-01-01	12345678910
         * delete 成功！
         *
         * 进程已结束，退出代码为 0
         */
    }
}
